import '../../../core/app_export.dart';
import '../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../widgets/app_bar/custom_app_bar.dart';
import '../../../widgets/app_bar/outline_icon_button.dart';
import '../../../widgets/screen_state_widget.dart';
import 'package:flutter/material.dart';

import '../widgets/attendance_item_view.dart';
import '../widgets/date_range_picker_widget.dart';
import 'attendances_list_cubit.dart';

class AttendancesListScreen extends StatelessWidget {
  final DateTime? startDate;
  final DateTime? endDate;
  final String? exerciseId;
  const AttendancesListScreen({
    Key? key,
    this.startDate,
    this.endDate,
    this.exerciseId,
  }):super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    var attendancesCubit = AttendancesListCubit();
    return Scaffold(
      appBar:_buildAppBar(context:context ,cubit: attendancesCubit),
      body: BlocProvider<AttendancesListCubit>(
        create: (context) => attendancesCubit..init( startDate: startDate??DateTime(2000) , endDate: endDate??DateTime(5000), exerciseId:exerciseId ,),
        child: BlocConsumer<AttendancesListCubit, AttendancesListState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = AttendancesListCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: ScreenStateWidget(
                isDone: state is AttendancesListDone,
                isError: false,
                isLoading: state is AttendancesListLoading,
                onRefresh: cubit.getAllAttendances,
                onDone: Column(
                  children: [
                    if(startDate!= null && endDate != null)
                    DateRangePickerWidget(startDate: startDate,endDate: endDate,),
                    Expanded(
                      child:cubit.attendances.isEmpty?
                      Center(
                       child: Text('لم يتم العثور على تسجيلات حضور',style:theme.textTheme.titleMedium),
                      )
                      :ListView.builder(
                        itemCount:cubit.attendances.length,
                        itemBuilder: (context,index){
                          return AttendanceItemView(attendance:cubit.attendances[index]);
                        },
                      ),
                    ),
                  ],
                ),
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context, required AttendancesListCubit cubit}) {
    return CustomAppBar(
        actions: [

        ],
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "سجل الحضور", margin: EdgeInsets.only(left: 12.h)));
  }

}
